/**
 * Created by fkjslee on 2018/3/15.
 */

//var paramUrl = "http://127.0.0.1:8081";
var localUrl = "http://118.24.119.103:8081";
var HTTP = "http://"

//initialize some label of select, many selector will not work without this
$(document).ready(function () {
    $("#agentsName").combobox({
        valueField: "id",
        textField: "text",
        onChange: function (n, o) {
            getFieldsName();
        }
    });
    $("#fieldsName").combobox({
        valueField: "id",
        textField: "text",
        onChange: function (n, o) {
            getNewValue();
        }
    });
    $("#discreteValues").combobox({
        multiple: true,
        valueField: "id",
        textField: "text"
    })
});

function showDlgChoose() {
    // $("#dlgChooseAgentField").dialog({modal: true});
    $("#dlgChooseAgentField").dialog("open");
    getAgentsName();
    $("#agentsName").combobox("select", 0);
}

//get agents name when the html was loaded
function getAgentsName() {
    var request = new XMLHttpRequest();
    request.open("POST", HTTP + window.location.host + "/getParam?type=agentsName");
    request.send();
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            $("#agentsName").combobox({
                data: JSON.parse(request.responseText),
                onLoadSuccess: function () {
                    $("#agentsName").combobox("select", 0);
                    getFieldsName();
                }
            })
        }
    }
}

//fill fields name when finish selecting agent
function getFieldsName() {
    var request = new XMLHttpRequest();
    request.open("POST", HTTP + window.location.host + "/getParam?type=fieldsName&agentName=" + $("#agentsName").combobox("getText"));
    request.send();
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            $("#fieldsName").combobox({
                data: JSON.parse(request.responseText),
                onLoadSuccess: function () {
                }
            })
        }
    }
}

//ask user to input new value when select both agent and field
function getNewValue() {
    var request = new XMLHttpRequest();
    request.open("POST", HTTP + window.location.host + "/getParam?type=newValue&agentName=" + $("#agentsName").combobox("getText") + "&fieldName=" + $("#fieldsName").combobox("getText"));
    request.send();
    request.onreadystatechange = function () {
        var type;
        if(request.readyState === 4 && request.status === 200) {
            var json = JSON.parse(request.responseText);
            for(var i in json) {
                type = i;
            }
            if(type === "HyperParam") {
                openDlgChooseHyper(json);
            } else if (type === "ContinuousParam") {
                openDlgChooseContinuous(json);
            } else if (type === "DiscreteParam") {
                openDlgChooseDiscrete(json);
            } else {
                alert("getNewValue no this type!");
            }
        }
    }
}

